package com.tjetc.dao;

import com.tjetc.domain.Boss;
import com.tjetc.utils.Db;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class BossDao {
    private QueryRunner runner=new QueryRunner();
    public Boss log(String username, String password) throws SQLException {
        return Db.runner.query("select * from boss where username=? and password=?",new BeanHandler<>(Boss.class),username,password);
    }

    public Boss findBossByBossId(int bossId) throws SQLException {
        return Db.runner.query("select * from boss where id=?",new BeanHandler<>(Boss.class),bossId);
    }

    public boolean updateUsername(String newUsername, int bossId) throws SQLException {
        int i = Db.runner.update(Db.getConnection(), "update boss set username=? where id=?", newUsername, bossId);
        return i!=0;
    }

    public boolean updatePassword(String newPassword, int bossId) throws SQLException {
        int i = Db.runner.update(Db.getConnection(), "update boss set password=? where id=?", newPassword, bossId);
        return i!=0;
    }

    public boolean updateName(String newName, int bossId) throws SQLException {
        int i = Db.runner.update(Db.getConnection(), "update boss set name=? where id=?", newName, bossId);
        return i!=0;
    }

    public boolean updatePhone(String newPhone, int bossId) throws SQLException {
        int i = Db.runner.update(Db.getConnection(), "update boss set phone=? where id=?", newPhone, bossId);
        return i!=0;
    }

    public Boss findBossByusername(String username) throws SQLException {
        return Db.runner.query("select * from boss where username=?",new BeanHandler<>(Boss.class),username);
    }
}
